package cn.wenhaha.app.log.service.imp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.wenhaha.app.log.bean.vo.LogHomeVo;
import cn.wenhaha.app.log.bean.vo.MappingState;
import cn.wenhaha.app.log.bean.vo.ReportWeekHome;
import cn.wenhaha.app.log.service.SyncReportService;
import cn.wenhaha.common.db.bean.SysMapping;
import cn.wenhaha.common.db.bean.SysSyncReport;
import com.jfinal.kit.Kv;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-12-20 20:58
 */
@Service
public class SyncReportServiceImp  implements SyncReportService {

    @Resource
    private SysSyncReport report;

    @Resource
    private SysMapping sysMapping;

    @Override
    public LogHomeVo simpleCount() {
        Map<String, Object> dataMap = report.basicCount();

        return LogHomeVo.builder()
                .success(MapUtil.getLong(dataMap, "success"))
                .fail(MapUtil.getLong(dataMap, "fail"))
                .mappingNumber(MapUtil.getInt(dataMap, "mappingNumber"))
                .yearCountNumber(MapUtil.getLong(dataMap, "yearCountNumber")).build();
    }

    @Override
    public ReportWeekHome reportWeek(Integer id) {
        Date date = new Date();
        Date startDate = DateUtil.beginOfWeek(date).toJdkDate();
        Long[]  success = new Long[7];
        Long[]  fail = new Long[7];
        Map<String, List<SysSyncReport>> reportList = report.reportWeek(id)
                .stream().collect(Collectors.groupingBy(r -> r.getStr("date")));

        for (int i = 0; i < 7; i++) {
            Date date1 = DateUtil.offsetDay(startDate, i).toJdkDate();
            List<SysSyncReport> sysSyncReports = reportList.get(DateUtil.formatDate(date1));
            if (CollUtil.isEmpty(sysSyncReports)){
                success[i] = 0L;
                fail[i] = 0L;
            }else {
                SysSyncReport sysSyncReport = sysSyncReports.get(0);
                success[i] = sysSyncReport.getLong("success");
                fail[i] = sysSyncReport.getLong("fail");
            }
        }

        return new ReportWeekHome(Arrays.asList(fail),Arrays.asList(success));
    }

    @Override
    public List<Kv> mappingList() {
        List<SysMapping> bySimpleList = sysMapping.findBySimpleList();

        return bySimpleList.stream().map(r -> Kv.by("label", r.getStr("name"))
                 .set("value", r.getInt("id"))).collect(Collectors.toList());
    }


    /**
     * <p>
     *    查询最近的更新对象
     *     - 最近十条
     * </p>
     * @Author: Wyndem
     * @DateTime: 2023-01-18 21:32
     */
    @Override
    public List<MappingState> state() {

        return report.state().stream().map(m -> {
            MappingState mappingState = new MappingState();
            mappingState.setId(m.getId());
            mappingState.setName(m.getStr("name"));
            mappingState.setDataName(m.getNameData());
            mappingState.setUploadName(m.getNameUpload());
            mappingState.setUpdateTime(m.getUpdateDate());
            return mappingState;
        }).collect(Collectors.toList());

    }
}
